<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Form Pricessing</title>
    <style type="text/css">
        em {
            color: red;
        }

    </style>
</head>

<body>
<div id="wrapper">
    <?php
    if (isset($_POST['submit'])) {
        $required = array('item', 'quantity', 'price', 'tax');
        $errors = array();
        foreach ($required as $fieldname) {
            if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
                $errors[] = "<em>" . $fieldname . '</em> cannot be blank.<br/>';
            }
        }


        $numeric = array('quantity', 'price', 'tax');
        foreach ($numeric as $fieldname) {
            if (!isset($_POST[$fieldname]) || !is_numeric($_POST[$fieldname])) {
                $errors[] = 'Please fill <em>' . $fieldname . '</em> the valid number<br/>';
            }
        }

        //process the price

        $item = $_POST['item'];
        $quantity = $_POST['quantity'];
        $price = $_POST['price'];
        $tax = $_POST['tax'];
        //get total
        if (empty($errors)) {
            $total = $price * $quantity * $tax / 100;
        }
    }

    ?>
    <form action="" method="post">
        <p><strong>Item:</strong>
            <select name="item">
                <option value="">--Select--</option>
                <?php
                $items = array('book' => 'PHP', 'dvd' => 'Iron Man', 'movie' => 'Harry Potter');
                foreach ($items as $key => $value) {
                    echo "<option value='{$key}'";
                    if (isset($item) && $item == $key) {
                        echo "selected='selected'";
                    }
                    echo ">" . $value . "</option>";
                }
                ?>
            </select>
        </p>
        <p><strong>Quantity:</strong>
            <input type="text" name="quantity" value="<?php echo isset($quantity) ? $quantity : "" ?>" size="5"/>
        </p>

        <p><strong>Price:</strong>
            <input type="text" name="price" value="<?php echo isset($price) ? $price : "" ?>" size="5"/>
        </p>

        <p><strong>Tax:</strong>
            <input type="text" name="tax" value="<?php echo isset($tax) ? $tax : "" ?>" size="5"/>
        </p>

        <p>
            <input type="submit" name="submit" value="Calculate"/>
        </p>
    </form>
    <ul>
        <?php
        echo isset($total) ? "<strong>The total is :" . $total . "</strong>" : "";
        ?>
        <p>
            <?php if (!empty($errors)) {

                echo "<strong>Please fix the following " . count($errors) . ' errors.</strong>';
                foreach ($errors as $item) {
                    echo "<li>" . $item . "</li>";
                }

            } ?>
        </p>
    </ul>
</div>
</body>
</html>